package com.ruoyi.goods.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.goods.domain.Brand;
import com.ruoyi.goods.domain.BrandPerson;
import com.ruoyi.goods.mapper.BrandMapper;
import com.ruoyi.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public int getBrandIdByBrandPersonId(int brandPersonId) {
        return brandMapper.getBrandIdByBrandPersonId(brandPersonId);
    }

    @Override
    public List<Brand> getBarandList(int deptId) {
        return brandMapper.getBrandList(deptId);
    }

    @Override
    public List<BrandPerson> getBrandPersonList(int brandId) {
        return brandMapper.getBrandPersonList(brandId);
    }

    @Override
    public int insertBrand(Brand brand) {
        int count = brandMapper.getBrandCountByCode(brand.getCode(), brand.getId());
        if(count > 0){
            throw new ServiceException(String.format("该品牌代号已存在", ""));
        }
        return brandMapper.insertBrand(brand);
    }

    @Override
    public int insertBrandPerson(BrandPerson branchPerson) {
        int count = brandMapper.getBrandPersonCountByCode(branchPerson.getCode(), 0);
        if(count > 0){
            throw new ServiceException(String.format("该品牌系列代号已存在", ""));
        }
        return brandMapper.insertBrandPerson(branchPerson);
    }

    @Override
    public int updateBrand(Brand brand) {
        int count = brandMapper.getBrandCountByCode(brand.getCode(), brand.getId());
        if(count > 0){
            throw new ServiceException(String.format("该品牌代号已存在", ""));
        }
        return brandMapper.updateBrand(brand);
    }

    @Override
    public int updateBrandPerson(BrandPerson branchPerson) {
        int count = brandMapper.getBrandPersonCountByCode(branchPerson.getCode(), branchPerson.getId());
        if(count > 0){
            throw new ServiceException(String.format("该品牌系列代号已存在", ""));
        }
        return brandMapper.updateBrandPerson(branchPerson);
    }

    @Override
    public int delBrandByIds(Long[] ids) {
        int brandResult = 0;
        for (int i = 0;i < ids.length;i++){
            brandResult += brandMapper.getBrandPersonCount(Math.toIntExact(ids[i]));
        }
        if(brandResult > 0){
            throw new ServiceException(String.format("你删除的品牌含有品牌系列不能删除", ""));
        }
        return brandMapper.delBrandByIds(ids);
    }

    @Override
    public int delBrandPersonByIds(Long[] ids) {
        return brandMapper.delBrandPersonByIds(ids);
    }

    @Override
    public List<Brand> getBrandAndBrandPersonList(List<Brand> brands) {
        return brandMapper.getBrandAndBrandPersonList(brands);
    }
}
